#ifndef FENGGO_AI_LOG
#define FENGGO_AI_LOG

#include <dlfcn.h>
#include "FengGo.hpp"

namespace magicube{

extern long nowBoard[] ;

class log{

public:
	log(void){}
	~log(void){}
	
public:
	int ErrLog(const char * form,...){
		char sFileName[255 + 1];
		char pbString [255 + 1];
		va_list   arg;
		
		memset(sFileName, 0x00, sizeof(sFileName));
		sprintf(sFileName, "%s%s", getenv("HOME"), LOG_FILE);
		
		std::ofstream outfile(sFileName, std::ios::app);
		if(!outfile) {
			//cerr<<"open file error\n"; return 1;
		}
		
		va_start(arg,form);  
		vsprintf(pbString, form, arg);   //若此行和下行改为：done = fprintf(console_printf_f,form,arg);则可变
		outfile<<pbString;         //参数的值不能正常显示，原因是参数传递错误，fprintf不能正确处理va_list类型的参数
		va_end(arg);
		
		outfile.close();
		
		return 0;
	}

	int boardHistory(){
		char sFileName[255 + 1];
		int  i, j;
		
		memset(sFileName, 0x00, sizeof(sFileName));
		sprintf(sFileName, "%s%s", getenv("HOME"), HISTORY_FILE);
		
		std::ofstream outfile(sFileName, std::ios::app);
		if(!outfile) {
			//cerr<<"open file error\n"; return 1;
		}
		
		if(iReturn == 1){
			outfile << "棋手第[" << iStep << "]步[" << sCols << "][" << sLines << "]\n";
			iReturn *= -1;
			iStep++;
		} else {
			outfile<<"FengGo第["<<iStep<<"]步[" << sCols << "][" << sLines << "]\n";
			iReturn *= -1;
			iStep++;
		}
		
		for(i = 0; i < BOARD_LINES; i++){
			for(j = 0; j < BOARD_LINES; j++){
				outfile<<((nowBoard[i] >> (j * 2)) & 0x0000000000000003)<<"  ";         //参数的值不能正常显示，原因是参数传递错误，fprintf不能正确处理va_list类型的参数
			}
			outfile<<"\n";
		}
		
		outfile<<"---------------------------------------------------------\n";
		outfile.close();
		
		return 0;
	}
};

log  AIlog;
}
#endif
